<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Plus, Upload } from '@element-plus/icons-vue'

import pageContainer from '../../components/pageContainer.vue'
import { useUserStore } from '../../stores/index.ts'

const userStore = useUserStore()

onMounted(() => {
  console.log(userStore.user)
})

const imageUrl = ref(userStore.user.user_pic)

const uploadRef = ref()
const onSelectFile = (uploadFile) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw)
}
</script>

<template>
  <pageContainer title="更新头像">
    <el-upload
      ref="uploadRef"
      class="avatar-uploader"
      :show-file-list="false"
      :on-change="onSelectFile"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
    </el-upload>
    <el-button
      @click="uploadRef.$el.querySelector('input').click()"
      type="primary"
      :icons="Plus"
      size="large"
      >选择图片</el-button
    >
    <el-button type="success" :icons="Upload" size="large">上传头像</el-button>
  </pageContainer>
</template>

<style lang="scss" scoped>
.avatar-uploader {
  margin-bottom: 15px;
  :deep() {
    .avatar {
      width: 278px;
      height: 278px;
      display: block;
    }
    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }
    .el-upload:hover {
      border-color: var(--el-color-primary);
    }
    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 278px;
      height: 278px;
      text-align: center;
    }
  }
}
</style>
